﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.CommonMethodServiceRef;

    public class AuditConfigure : BasePage
    {
        protected Button btnGoBack;
        protected Button btnSave;
        protected HtmlForm F1211;
        protected GridView gvAuditConfigure;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnTableName;
        protected Label lblAllocateRule;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("AuditConfigure.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("AuditConfigureList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                List<WMS6.Web.CommonMethodServiceRef.AuditDefinitionInfo> auditDefinitionInfos = new List<WMS6.Web.CommonMethodServiceRef.AuditDefinitionInfo>();
                WMS6.Web.CommonMethodServiceRef.AuditDefinitionInfo auditDefinitionInfo = null;
                for (int i = 0; i < this.gvAuditConfigure.Rows.Count; i++)
                {
                    auditDefinitionInfo = new WMS6.Web.CommonMethodServiceRef.AuditDefinitionInfo {
                        ColName = this.gvAuditConfigure.DataKeys[i]["ColName"].ToString(),
                        TableName = this.gvAuditConfigure.DataKeys[i]["TableName"].ToString(),
                        ColType = this.gvAuditConfigure.DataKeys[i]["ColType"].ToString(),
                        ForInsert = ((CheckBox) this.gvAuditConfigure.Rows[i].FindControl("chbForInsert")).Checked ? "Y" : "N",
                        ForUpdate = ((CheckBox) this.gvAuditConfigure.Rows[i].FindControl("chbForUpdate")).Checked ? "Y" : "N",
                        ForDelete = ((CheckBox) this.gvAuditConfigure.Rows[i].FindControl("chbForDelete")).Checked ? "Y" : "N"
                    };
                    auditDefinitionInfos.Add(auditDefinitionInfo);
                }
                if ((auditDefinitionInfos != null) && (auditDefinitionInfos.Count > 0))
                {
                    this.lblMsg.Text = CommoneMethodServiceClientProxy.GetInstance().UpdateAuditDefinitionInfoByList(auditDefinitionInfos) + base.GetGlobalMsgResource("F1910_001");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void gvAuditConfigure_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                Control control = e.Row.FindControl("chbForInsert");
                if ((control != null) && (control is CheckBox))
                {
                    ((CheckBox) control).Checked = ((CheckBox) control).ToolTip == "Y";
                }
                Control control1 = e.Row.FindControl("chbForUpdate");
                if ((control1 != null) && (control1 is CheckBox))
                {
                    ((CheckBox) control1).Checked = ((CheckBox) control1).ToolTip == "Y";
                }
                Control control2 = e.Row.FindControl("chbForDelete");
                if ((control2 != null) && (control2 is CheckBox))
                {
                    ((CheckBox) control2).Checked = ((CheckBox) control2).ToolTip == "Y";
                }
                Control control3 = e.Row.FindControl("chbIsPrimary");
                if ((control3 != null) && (control3 is CheckBox))
                {
                    ((CheckBox) control3).Checked = ((CheckBox) control3).ToolTip == "Y";
                }
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "CheckBoxClick(this,'" + ((CheckBox) control).ClientID + "','" + ((CheckBox) control1).ClientID + "','" + ((CheckBox) control2).ClientID + "')");
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnTableName.Value = base.Request.QueryString["ID"].ToString();
                this.setAuditConfigure();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void setAuditConfigure()
        {
            try
            {
                string strTableName = this.hdnTableName.Value;
                List<object> objs = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoListByID(strTableName, "", "AUDIT_DEFINITION");
                if (objs != null)
                {
                    List<WMS6.Web.BaseInfoDetailServiceRef.AuditDefinitionInfo> auditDefinitionInfos = null;
                    if ((objs != null) && (objs.Count > 0))
                    {
                        auditDefinitionInfos = new List<WMS6.Web.BaseInfoDetailServiceRef.AuditDefinitionInfo>();
                        for (int i = 0; i < objs.Count; i++)
                        {
                            auditDefinitionInfos.Add((WMS6.Web.BaseInfoDetailServiceRef.AuditDefinitionInfo) objs[i]);
                        }
                    }
                    base.BindData2GridView(this.gvAuditConfigure, auditDefinitionInfos);
                    base.DataEntityViewState = auditDefinitionInfos;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

